package com.feisuanyz.lyweb01.service.impl;

import com.feisuanyz.lyweb01.dao.UserMapper;
import com.feisuanyz.lyweb01.pojo.dto.UserDTO;
import com.feisuanyz.lyweb01.pojo.query.UserQuery;
import com.feisuanyz.lyweb01.service.UserService;
import java.lang.Boolean;
import java.lang.Override;
import java.lang.String;
import javax.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.feisuanyz.lyweb01.exception.BusinessException;
import java.util.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feisuanyz.lyweb01.pojo.domain.UserDO;
import java.util.Map;
import com.feisuanyz.lyweb01.constant.ResultCodeConstant;
import java.util.Date;

/**
 * 用户管理的实现
 *
 * @author deng
 * @date 2025-02-26 11:57:37
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean registerUser(UserDTO userDTO) {
        // 验证username是否唯一
        QueryWrapper<UserDO> queryWrapper = Wrappers.query();
        queryWrapper.eq("username", userDTO.getUsername());
        UserDO existingUser = userMapper.selectOne(queryWrapper);
        if (existingUser != null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 插入新的用户记录
        UserDO userDO = new UserDO();
        userDO.setUsername(userDTO.getUsername());
        userDO.setPassword(userDTO.getPassword());
        userDO.setEmail(userDTO.getEmail());
        userDO.setCreateTime(new Date());
        userMapper.insert(userDO);
        return true;
    }

    @Override
    public Boolean loginUser(UserQuery userQuery) {
        // 查询用户记录
        QueryWrapper<UserDO> queryWrapper = Wrappers.query();
        queryWrapper.eq("username", userQuery.getUsername());
        UserDO userDO = userMapper.selectOne(queryWrapper);
        // 验证用户密码是否正确
        if (userDO != null && userDO.getPassword().equals(userQuery.getPassword())) {
            return true;
        } else {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
    }

    @Override
    public Boolean recoverPassword(UserQuery userQuery) {
        // 查询用户记录
        QueryWrapper<UserDO> queryWrapper = Wrappers.query();
        queryWrapper.eq("username", userQuery.getUsername());
        UserDO userDO = userMapper.selectOne(queryWrapper);
        // 验证用户email是否匹配
        if (userDO != null && (userDO.getEmail() == null || userDO.getEmail().equals(userQuery.getEmail()))) {
            // 生成新的密码并发送到用户邮箱
            String newPassword = UUID.randomUUID().toString().substring(0, 8);
            userDO.setPassword(newPassword);
            userDO.setUpdateTime(new Date());
            userMapper.updateById(userDO);
            try {
                sendPasswordToEmail(userDO.getEmail(), newPassword);
                return true;
            } catch (MessagingException e) {
                throw new BusinessException(ResultCodeConstant.CODE_999999, ResultCodeConstant.CODE_999999_MSG);
            }
        } else {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
    }

    private void sendPasswordToEmail(String email, String password) throws MessagingException {
        // 这里使用Spring的邮件发送功能
        // 由于示例中没有具体实现邮件发送，这里只做抛出异常的模拟
        throw new MessagingException("邮件发送失败");
    }
}
